package com.gongzonqiang.controller;

import java.io.File;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

@RestController
@RequestMapping("/img")
public class FileController {


	// 图片服务器地址
	@Value("${wx.imagesHost}")
	private String imageServiceHost;
	// 图片服务器地址
	@Value("${wx.imagesPath}")
	private String imagesPath;

	@RequestMapping("/upload")
	public String upload(@RequestParam("imgs") MultipartFile imgs, HttpServletRequest request) {
		String saveName = null;
		String contentType = imgs.getContentType();
		String fileName = imgs.getOriginalFilename();
		System.out.println("fileName-->" + fileName);
		System.out.println("getContentType-->" + contentType);
		try {
			saveName = UUID.randomUUID().toString().replace("-", "") + fileName.substring(fileName.lastIndexOf("."));
			imgs.transferTo(new File(imagesPath + saveName));
		} catch (Exception e) {
		}
		// 返回json
		return imageServiceHost + saveName;

	}

	/**
	 * 删除文件（添加文件的时候删除）
	 */
	@RequestMapping("/delFile")
	public void delFile(String fileName) {
		fileName = fileName.substring(fileName.lastIndexOf("/"));
		File file = new File(imagesPath + fileName);
		file.delete();
	}

}
